<script lang="ts" setup>
import type { FormInstance, FormRules } from "element-plus";
import { reactive, ref } from "vue";

type Props = {
  modelValue: boolean;
  title: string;
  type: number;
  formData: any;
};
const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  title: "",
  type: 1,
  formData: {
    name: "",
    currency_type: "",
    price: null,
    notes: ""
  }
});

// const formDataMap = computed(() => {
//   const obj = { ...props.formData };
//   return obj;
// });

const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules>({
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  unit: [{ required: true, message: "请输入单位", trigger: "blur" }],
  currency_type: [{ required: true, message: "请输入币种", trigger: "blur" }],
  price: [{ required: true, message: "请输入价格", trigger: "change" }]
});

const emit = defineEmits(["update:modelValue", "init-form", "submit-form"]);

const inputHandle = val => {
  emit("update:modelValue", val);
};

const inputWidth = "200px";

const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(valid => {
    if (valid) {
      let obj;
      if (props.type === 1) {
        obj = { ...props.formData };
      } else {
        const { id, currency_type, price, notes } = props.formData;
        obj = { id, currency_type, price, notes };
      }
      emit("submit-form", obj);
    } else {
      console.log("error submit!");
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  inputHandle(false);
};
</script>

<template>
  <el-dialog
    :modelValue="modelValue"
    @update:modelValue="inputHandle"
    :title="title"
    width="40%"
  >
    <span>
      <el-form
        :inline="true"
        :model="props.formData"
        label-width="auto"
        label-position="left"
        ref="ruleFormRef"
        :rules="rules"
      >
        <el-form-item label="服务名称:" prop="name">
          <el-input
            v-model="props.formData.name"
            :style="{ width: inputWidth }"
            :disabled="title === '修改服务' ? true : false"
          />
        </el-form-item>
        <el-form-item label="币种:" prop="currency_type">
          <el-input
            v-model="props.formData.currency_type"
            :style="{ width: inputWidth }"
          />
        </el-form-item>
        <el-form-item label="价格:" prop="price">
          <el-input-number
            v-model="props.formData.price"
            :min="0"
            :precision="2"
            :step="0.1"
            :style="{ width: inputWidth }"
          />
        </el-form-item>
        <el-form-item label="备注:">
          <el-input
            v-model="props.formData.notes"
            :style="{ width: inputWidth }"
          />
        </el-form-item>
      </el-form>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm(ruleFormRef)">取 消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
